package com.eascs.finance.admin.web.controller;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.finance.admin.common.controller.BaseMasterViewController;
import com.eascs.finance.admin.common.entity.ArticleItemEntity;
import com.eascs.finance.admin.common.vo.ArticleItemQueryVO;
import com.eascs.finance.admin.web.service.ArticleItemServiceImpl;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.context.RequestContext;

@Controller
@RequestMapping("/articleitem")
public class ArticleItemController extends BaseMasterViewController {
	
	@Inject
	ArticleItemServiceImpl articleItemService;
	
	
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "propagandaManage");
	}
	
	@RequestMapping(value="/index")
	public String index(ArticleItemQueryVO vo){
		vo.setPageSize(10);
		PageResult<Object[]> pageResult= articleItemService.findArticleItemInfo(vo);
	  
        String[] titles = {"栏目名称", "发布时间", "排序",  "发布人","状态"};
        AjaxConfirmButton add =new AjaxConfirmButton("  发布", "/articleItemRest/updateStatus?status=2&id=");
        add.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "禁用".equals(params[5])||"新建".equals(params[5]);
			}
		});
        
        AjaxConfirmButton close =new AjaxConfirmButton(" 禁止", "/articleItemRest/updateStatus?status=0&id=");
        close.setShowButtonFilter(new ShowButtonFilter() {
			
			@Override
			public boolean filter(Object param) {
				Object[] params=(Object[]) param;
				return "发布".equals(params[5]);
			}
		});
        RedirectButton update =new RedirectButton("  编辑", "/articleitem/edit?id=");
        AjaxConfirmButton del =new AjaxConfirmButton("  删除", "/articleItemRest/delete?id=");
		Button[] detailButton = new Button[] { add,close,update,del };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setUrl(UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "showPage", ""));
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		BaseRestResult.refresh();
		return "/articleitem/index";
		
		
	}
	
	
	@RequestMapping(value="/add")
	public String add(){
		return "/articleitem/article_item_edit";
	}
	
	
	@RequestMapping(value="/edit")
	public String edit(String id){
		ArticleItemEntity item=articleItemService.findArticleItemById(id);
		ViewContext.put("vo", item);
		return "/articleitem/article_item_edit";
	}
	
}
